<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Cshare extends controller {
	public function dotables() {
		$view  = C\view::inst('json');
		Cadmin::islogin($view);
		$orm   = input::post('orm');
		$list  = C\model::load($orm)->db_table();
		array_shift($list);
		$tables= array();
		foreach ($list as $table) {
			$tables[] = array('id'=>$table, 'name'=>$table);
		}
		$view->assign_by_ref('dataset', $tables);
		$view->display();
	}
	
	public function douser() {
		$view  = C\view::inst('json');
		Cadmin::islogin($view);
		list($start, $limit, $sorts) = Cadmin::offset();
		$query = input::post('query');
		$where = array();
		if ($query) {
			$where = array(array('account', '%'.$query.'%', \classlib\orm::OP_LIKE),
				array('nickname', '%'.$query.'%', \classlib\orm::OP_LIKE, \classlib\orm::OP_OR),
				array('english', '%'.$query.'%', \classlib\orm::OP_LIKE, \classlib\orm::OP_OR)
			);
		}
		$orm   = C\model::load('main.sys.user');
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$field = 'id, account as name, nickname';
			$list  = $orm->get_list($start, $limit, $where, $field, $sorts);
			foreach ($list as &$item) {
				if (!empty($item['nickname'])) {
					$item['name'] .= '['.$item['nickname'].']';
				}
				unset($item['nickname']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	public function dorole() {
		$view  = C\view::inst('json');
		Cadmin::islogin($view);
		list($start, $limit, $sorts) = Cadmin::offset();
		$query = input::post('query');
		$query = trim($query);
		$where = array();
		if ($query) {
			$where[] = array('name', '%'.$query.'%', \classlib\orm::OP_LIKE);
		}
		$orm   = C\model::load('main.sys.role');
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$field = 'id, name';
			$list  = $orm->get_list($start, $limit, $where, $field, $sorts);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	public function doacl() {
		$view   = C\view::inst('json');
		$userid = Cadmin::islogin($view);
		$url    = input::post('url');
		$access = true;
		if ($url) {
			$access = Cadmin::url_access($userid, $url);
		}
		$view->assign_by_ref('access', $access);
		$view->display();
	}
}
